//
//  CHCompanyQueryController.m
//  CarFile
//
//  Created by 陈浩 on 2018/2/7.
//  Copyright © 2018年 陈浩. All rights reserved.
//

#import "CHCompanyQueryController.h"
#import "CHRepairCell.h"
#import "QY_Model.h"
@interface CHCompanyQueryController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) int page;
@end

@implementation CHCompanyQueryController

-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"企业查询";
    _page = 0;
    [self setupTableView];
    
    [self getData];
    
}


-(void)getData{
    _page = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = @"410100000000000000";
    params[@"company"] = @"";
    params[@"magorbrands"] = @"";
    params[@"user"] = [Globle getInstance].user;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"sign"] = [DESCript encrypt:@"410100000000000000" encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    
    
    NSLog(@"%@",params);
    [[Globle getInstance].service requestWithServiceName:@"lcipappsearchcompanys" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        NSLog(@"维修企业%@",result);
        [MBProgressHUD hideHUD];
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                QY_Model *model = [QY_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
        }
        self.tableView.placeholderImage = @"data_null";
        self.tableView.placeholder = @"非常抱歉，没有找到相关维修企业";
        [self.tableView reloadData];
    }];
    
}

-(void)loadMoreData{
    _page++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"company"] = @"";
    params[@"magorbrands"] = @"";
    params[@"user"] = [Globle getInstance].user;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovcompanylist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [MBProgressHUD hideHUD];
        [self.tableView.mj_footer endRefreshing];
      
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                QY_Model *model = [QY_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            
            [self.tableView reloadData];
        }
    }];
    
}




-(void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        
        [self getData];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
    [self.view addSubview:self.tableView];
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHRepairCell *cell = [CHRepairCell cellForTableView:tableView];
    cell.model = self.datas[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 130;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
